
import sys
from PyQt4 import QtGui, QtCore
from box import *
from enum_classes import *

class dic_entry :

    def __init__(self):
        self.value = -1
        self.dep_list = []  

  

class window :
    def __init__(self, root_box):
      self.root_box = root_box
      self.root_box.prepare()
      self.var_dict = {}
      self.event_dict = {}
      self.init_var_dict()
      self.init_event_dict()



    def find_var_owner( self, current_box, var_name ) :
      if ( current_box.vars.has_key(var_name) ): 
         return current_box
      for child in current_box.children :
         if ( child.vars.has_key(var_name) ) :
            return child
         if ( var_name in child.child_vars ) :
            return self.find_var_owner( child, var_name )


    def run_Gui_Func(self, owner, var_name, new_value):
        var_type = owner.vars[var_name]
        if ( var_type == GuiVariables.TEXT ):
            owner.setText(new_value)
        elif ( var_type == GuiVariables.BGCOLOR ):
            owner.setBGColor(new_value)
        elif ( var_type == GuiVariables.CHECKBOX_VAL ):
            owner.widget.setCheckState(new_value)
        elif ( var_type == GuiVariables.RADIOBUTTON_VAL ):
            owner.widget.setChecked(new_value)
        elif ( var_type == GuiVariables.HEIGHT ):
            g = owner.widget.geometry()
            g.setHeight(new_value)
            owner.widget.setGeometry(g)
        elif ( var_type == GuiVariables.WIDTH ):
            g = owner.widget.geometry()
            g.setWidth(new_value)
            owner.widget.setGeometry(g)    
        elif ( var_type == GuiVariables.X_POS ):
            g = owner.widget.geometry()
            g.setX(new_value)
            owner.widget.setGeometry(g)
        elif ( var_type == GuiVariables.Y_POS ):
            g = owner.widget.geometry()
            g.setY(new_value)
            owner.widget.setGeometry(g)
        elif ( var_type == GuiVariables.SLIDER_VAL ):
            owner.widget.setValue(new_value)
        elif ( var_type == GuiVariables.SPLITTER_SIZE ):
            owner.widget.movSplitter(new_value,0)
        elif ( var_type == GuiVariables.SETMASK ):
            owner.widget.setMask(QtGui.QBitmap(new_value))
        elif ( var_type == GuiVariables.SETIMAGE ):
            pix = QtGui.QPixmap(new_value)
            pix = pix.scaled(owner.coordinates.w, owner.coordinates.h, QtCore.Qt.IgnoreAspectRatio, QtCore.Qt.SmoothTransformation)
            owner.widget.setPixmap(pix)
        elif ( var_type == GuiVariables.COMBOBOX_VAL ):
            owner.widget.currentIndex = new_value

            
    def get_Gui_Var(self, var_name):
        owner = self.find_var_owner( self.root_box, var_name )
        var_type = owner.vars[var_name]
        if ( var_type == GuiVariables.TEXT ):
            return owner.getText()
        elif (var_type == GuiVariables.CHECKBOX_VAL):
            return owner.isChecked()
        elif (var_type == GuiVariables.RADIOBUTTON_VAL):
            return owner.isChecked()    
        elif ( var_type == GuiVariables.HEIGHT ):
            g = owner.widget.geometry()
            return g.height()
        elif ( var_type == GuiVariables.WIDTH ):
            g = owner.widget.geometry()
            return g.width()
        elif ( var_type == GuiVariables.X_POS ):
            g = owner.widget.geometry()
            return g.x()
        elif ( var_type == GuiVariables.Y_POS ):
            g = owner.widget.geometry()
            return g.y()
        elif ( var_type == GuiVariables.SLIDER_VAL ):
            return owner.widget.value()
        elif ( var_type == GuiVariables.SPLITTER_SIZE ):
            return owner.widget.sizes()
        elif ( var_type == GuiVariables.COMBOBOX_VAL ):
            return owner.widget.currentIndex()
        
         
    def change_var( self, var_name, new_value):
      entry = self.var_dict[var_name]
      entry.value = new_value
      for f in entry.dep_list :
         f(self, entry.value, new_value)
      owner = self.find_var_owner( self.root_box, var_name )
      self.run_Gui_Func(owner, var_name, new_value )

    def init_var_dict(self):
      for v in self.root_box.vars :
         self.var_dict[v] = dic_entry()
      for v in self.root_box.child_vars :
         self.var_dict[v] = dic_entry()








    def find_event_owner( self, current_box, event_name ) :
      
      if ( current_box.events.has_key(event_name) ): 
         return current_box
      for child in current_box.children :
         if ( child.events.has_key(event_name) ) :
            return child
         if ( event_name in child.child_events ) :
            return self.find_event_owner( child, event_name )


    def event_runner(self, event_name):
      for f in self.event_dict[event_name] :
         f(self,event_name)


    def prepare_Gui_Event(self, owner, event_name):
        event_type = owner.events[event_name]
        if ( event_type == GuiEvents.CLICK ):
            QtCore.QObject.connect(owner.widget, QtCore.SIGNAL("clicked()"), lambda : self.event_runner(event_name) )
        elif ( event_type == GuiEvents.CHECKBOX_CHANGE ):
            QtCore.QObject.connect(owner.widget, QtCore.SIGNAL("stateChanged(int)"), lambda : self.event_runner(event_name))
        elif ( event_type == GuiEvents.RADIOBUTTON_PRESSED ):
            QtCore.QObject.connect(owner.widget, QtCore.SIGNAL("toggled(bool)"), lambda : self.event_runner(event_name))
        elif ( event_type == GuiEvents.SLIDE ):
            QtCore.QObject.connect(owner.widget, QtCore.SIGNAL("valueChanged(int)"), lambda : self.event_runner(event_name))
        elif ( event_type == GuiEvents.SPLITTER_MOVED ):
            QtCore.QObject.connect(owner.widget, QtCore.SIGNAL("splitterMoved(int,int)"), lambda : self.event_runner(event_name))
        elif ( event_type == GuiEvents.COMBOBOX_CHANGE ):
            QtCore.QObject.connect(owner.widget, QtCore.SIGNAL("currentIndexChanged(int)"), lambda : self.event_runner(event_name))
 
         


  #lambda who = "Three" : self.anyButton(who)


    def init_event_dict(self):
      for e in self.root_box.events :
         self.event_dict[e] = [] 
         self.prepare_Gui_Event( self.root_box, e)

      for e in self.root_box.child_events :
         self.event_dict[e] = []
         owner = self.find_event_owner(self.root_box, e)
         self.prepare_Gui_Event( owner, e)






    def when_changed( self, var_name, func ):
      self.var_dict[var_name].dep_list.append(func)

    def bind( self, event_name, func ):
      self.event_dict[event_name].append(func)


    def __call__( self, inp_dict ):
      for var in inp_dict :
         self.change_var(var,inp_dict[var])
      self.root_box.show()



            
         
